//公共方法集合
var ash ={
	showEditRow: function(){
		var content = $('.editrow');
		content.slideDown(500);
	},
	_datatable:{},
	initEdit:function(){
		//编辑按钮
		$(".dataEdit").each(function(){
			$(this).on("click",function(){
				$.ajax({
		            url : _ctx + "/admin/fang/youhui/"+$(this).attr("data"),
		            type : "get",
		            data : {},
		            dataType : "json",
		            success : function(returned) {
		            	if(returned.success){
		            		var data = returned.data;
						if(data.id)
						{
							$("#YouhuiForm input[name='id']").val(data.id);
						}
						else
							$("#YouhuiForm input[name='id']").val("");

						if(data.name)
						{
							$("#YouhuiForm input[name='name']").val(data.name);
						}
						else
							$("#YouhuiForm input[name='name']").val("");

						if(data.price)
						{
							$("#YouhuiForm input[name='price']").val(data.price);
						}
						else
							$("#YouhuiForm input[name='price']").val("");

						if(data.allnum)
						{
							$("#YouhuiForm input[name='allnum']").val(data.allnum);
						}
						else
							$("#YouhuiForm input[name='allnum']").val(0);

						if(data.getnum)
						{
							$("#YouhuiForm input[name='getnum']").val(data.getnum);
						}
						else
							$("#YouhuiForm input[name='getnum']").val(0);

						if(data.starttime)
						{
							$("#YouhuiForm input[name='starttime']").val(data.starttime);
						}
						else
							$("#YouhuiForm input[name='starttime']").val("");

						if(data.lasttime)
						{
							$("#YouhuiForm input[name='lasttime']").val(data.lasttime);
						}
						else
							$("#YouhuiForm input[name='lasttime']").val("");

						if(data.projectid)
						{
							$("#YouhuiForm #projectid").val(data.projectid);
						}
						else
							$("#YouhuiForm #projectid").val(-1);
						if(data.remark)
						{
							$("#YouhuiForm textarea[name='remark']").val(data.remark);
						}
						else
							$("#YouhuiForm textarea[name='remark']").val("");

			            ash.showEditRow();
		             }
	            	 else{
	            		layer.msg("数据获取失败.请联系系统管理员!");
	            	 }
		            },
		            erorr : function(e) {
		            	layer.msg(e.message());	
		            }
		        });
			});	
		});	
	}
};

$(function() {
	var _datatableurl =_ctx + "/admin/fang/youhui/data.json";
	if(projectid)
	{
		_datatableurl =_ctx + "/admin/fang/youhui/data.json?projectid="+projectid;
	}
	//加载数据
	ash._datatable = $(".youhuiDataTable").dataTable(
					{
						language : {
							"sProcessing" : "处理中...",
							"sLengthMenu" : "显示 _MENU_ 项结果",
							"sZeroRecords" : "没有匹配结果",
							"sInfo" : "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
							"sInfoEmpty" : "显示第 0 至 0 项结果，共 0 项",
							"sInfoFiltered" : "(由 _MAX_ 项结果过滤)",
							"sInfoPostFix" : "",
							"sSearch" : "搜索:",
							"sUrl" : "",
							"sEmptyTable" : "表中数据为空",
							"sLoadingRecords" : "载入中...",
							"sInfoThousands" : ",",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "上页",
								"sNext" : "下页",
								"sLast" : "末页"
							},
							"oAria" : {
								"sSortAscending" : ": 以升序排列此列",
								"sSortDescending" : ": 以降序排列此列"
							}
						},
						serverSide : true,
						"ajax" : _datatableurl,
						"columns" : [
								{
									"data" : "id",
									"width" : "4%",
									"render" : function(data) {
										return "<input type='checkbox' class='selectable-checkbox' name='id' data='"+data+"'>";
									}
								},
								{
									"data" : "id"
								},
								{
									"data" : "name"
								},
								{
									"data" : "price"
								},
								{
									"data" : "getnum"
								},
								{
									"data" : "allnum"
								},
								{
									"data" : "starttime"
								},
								{
									"data" : "lasttime"
								},
								{
									"data" : "id",
									"width" : "16%",
									"render" : function(data) {
										return "<a class='btn btn-xs btn-info dataEdit' data='"+data+"'><i class='fa fa-edit'></i>编辑</a> ";
									}
								}, ],
						deferRender : true
					});
	$('.youhuiDataTable').on('draw.dt',function() {
		ash.initEdit();
	});
	
	//全选按钮
	$(".checkAll").on("click",function(){
		$("input:checkbox[name='id']").each(function(){
			$(this).prop("checked","checked");
		});
	});
	//反选按钮
	$(".toggleCheck").on("click",function(){
		$("input:checkbox[name='id']").each(function(){
			if($(this).prop("checked"))
				$(this).prop("checked",false);
			else
				$(this).prop("checked",true);
		});
	});
	//新增按钮
	$(".addBtn").on("click",function(){
				$(".validate input[name='id']").val("");		
				$(".validate input[name='name']").val("");		
				$(".validate input[name='price']").val("");		
				$(".validate input[name='getnum']").val("");		
				$(".validate input[name='allnum']").val("");		
				$(".validate input[name='starttime']").val("");		
				$(".validate input[name='lasttime']").val("");		
				$(".validate input[name='remark']").val("");
				if(projectid)
				{
					$(".validate input[name='projectid']").val(projectid);
				}
				else
					$(".validate select[name='projectid']").val("-1");
				ash.showEditRow();
	});
	//删除按钮
	$(".delBtn").on("click",function(){
		 var checkedIds = "";
	        var _checked = $("input.selectable-checkbox:checked");
	        if(_checked.length==0)
	        {
	        	layer.msg("<H4><i class='fa fa-exclamation-circle'></i>警告!</H4><p>请勾选至少一条记录!</p>");
	        }
	        for (var i = 0; i < _checked.length; i++) {
	            checkedIds += $(_checked[i]).attr("data") + ",";
	        }
	        $.ajax({
	            url : _ctx + "/admin/fang/youhui/delete",
	            type : "post",
	            data : {
	                id : checkedIds
	            },
	            dataType : "json",
	            success : function(returned) {
	            	layer.msg("<H4><i class='fa fa-check'></i></H4><p>删除成功!</p>");
	               	ash._datatable.fnDraw(); 
	               //	$.fn.zTree.init($("#youhuiTree"), ztree.settings);
	            },
	            erorr : function(e) {
	            	layer.msg(e.message());	
	            }
	        });
	});
	$(".resetAddForm").on("click",function(){
		$("#YouhuiForm")[0].reset();
		$(".editrow").slideUp(500);
	})
	$(".validate").validate({
		submitHandler: function(form) {
			$.ajax({
				url:$(form).attr("action"),
				type:$(form).attr("method"),
				data:{
					id:$(".validate input[name='id']").val(),		
					name:$(".validate input[name='name']").val(),		
					price:$(".validate input[name='price']").val(),		
					getnum:$(".validate input[name='getnum']").val(),		
					allnum:$(".validate input[name='allnum']").val(),		
					starttime:$(".validate input[name='starttime']").val(),		
					lasttime:$(".validate input[name='lasttime']").val(),		
					projectid:$(".validate #projectid").val(),	
					remark:$(".validate textarea[name='remark']").val(),		
				},
				datatype:"json",
				success:function(returned){
					if(returned.success)
					{
						layer.msg(returned.msg);
					}
					else
						layer.msg(returned.msg);
					$("#YouhuiForm")[0].reset();
					$(".editrow").slideUp(500);
					ash._datatable.fnDraw(); 
					//$.fn.zTree.init($("#youhuiTree"), ztree.settings);
				},
				error:function(error){
					layer.msg(error);
				}
			})
		}
	});
});